/**
 * 给你一个整数 n ，请你生成并返回所有由 n 个节点组成且节点值从 1 到 n 互不相同的不
 * 同 二叉搜索树 。可以按 任意顺序 返回答案。
 * 
 * 
 * 1 <= n <= 8
 */

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<TreeNode*> generateTrees(int start,int end){
        if(start > end)return {nullptr};
        vector<TreeNode*> allTrees;
        for(int i = start;i <= end;i++){
            auto leftTree = generateTrees(start,i - 1);
            auto rightTree = generateTrees(i + 1,end);
            for(auto& left : leftTree){
                for(auto& right : rightTree){
                    auto currTree = new TreeNode(i);
                    currTree->left = left;
                    currTree->right = right;
                    allTrees.push_back(currTree);
                }
            }
        }
        return allTrees;
    }
    vector<TreeNode*> generateTrees(int n) {
        if(n == 0){
            return {};
        }
        return generateTrees(1,n);
    }
};